package ossRW

import (
	"feng-che.com/server/driver-quiz/myApp"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"log"
)

var OssClient *oss.Client

type ossSetting struct {
	EndPoint        string
	BucketName      string
	AccessKeyId     string
	AccessKeySecret string
}

var OssBucketName string
var OssBucketPrefix string

// 阿里云oss sdk 初始化
func AliyunOssClientInit() {
	aliSetting := SwitchAliyunSetting()
	OssBucketName = aliSetting.BucketName
	var createErr error
	OssClient, createErr = oss.New(
		aliSetting.EndPoint,
		aliSetting.AccessKeyId,
		aliSetting.AccessKeySecret)
	if createErr != nil {
		println("OSS的服务实例启动失败")
		println(createErr.Error())
		log.Fatal("启动中断，OSS实例没能启动")
	}
}

// 阿里云配置
func SwitchAliyunSetting() *ossSetting {
	setting := new(ossSetting)
	setting.EndPoint = myApp.AppSetting.Oss.EndPoint
	setting.BucketName = myApp.AppSetting.Oss.BucketName
	setting.AccessKeyId = myApp.AppSetting.Oss.AccessKeyId
	setting.AccessKeySecret = myApp.AppSetting.Oss.AccessKeySecret
	OssBucketPrefix = "https://" + setting.BucketName + "." + setting.EndPoint + "/"
	return setting
}
